<!DOCTYPE html>

<html>
	<head>
		<meta charset="UTF-8">
		<title>按字母顺序升序排列 </title>
	</head>
	<body>
		<p>数组：Banana、Orange、Apple、Mango</p>
		<p id="demo">单击按钮升序排列数组</p>
		<button onclick="myFunction()">点我</button>
		<script>
			var fruits=["Banana","Orange","Apple","Mango"];
			function myFunction(){
				for(var i=0;i<fruits.length;i++){
					for (var j=0;j<fruits.length-i-1;j++) {
						if(fruits[j].charCodeAt(0)>fruits[j+1].charCodeAt(0)){
							var a=fruits[j];
							fruits[j]=fruits[j+1];
							fruits[j+1]=a;
						}
					}
				}
				for(var b=0;b<fruits.length;b++){
					document.write(fruits[b]+"<br />");
				}
			}
		</script>
	</body>
</html>
